#include "mycoin.h"
#include <QDebug>
#include <QPixmap>
#include <QSize>

MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
{

}
MyCoin::MyCoin(QString butImg)
{


    QPixmap pixmap;
    bool ret = pixmap.load(butImg);
    if(!ret){//资源加载失败
        qDebug() << butImg <<"加载图片失败";

    }
    //设置金币固定大小
    this->setFixedSize(pixmap.width(),pixmap.height());
    //设置不规则
    this->setStyleSheet("QPushButton{border:0px}");
    //设置icon
    this->setIcon(pixmap);
    //设置icon大小
    this->setIconSize(QSize(pixmap.width(),pixmap.height()));

    //初始化定时器
    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    connect(timer1,&QTimer::timeout,[=](){
        QPixmap pixmap;
        QString str = QString(":/res/Coin000%1.png").arg(this->min++);
        pixmap.load(str);
        this->setFixedSize(pixmap.width(),pixmap.height());
        this->setStyleSheet("QPushButton{border:0xp}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));

        if(this->min > this->max) //如果大于最大值，重置最小值，并停止定时器
        {
            this->min = 1;
            this->isAnimation = false;
            timer1->stop();
        }
    });

    connect(timer2,&QTimer::timeout,[=](){
            QPixmap pixmap;
            QString str = QString(":/res/Coin000%1.png").arg((this->max)-- );
            pixmap.load(str);
            this->setFixedSize(pixmap.width(),pixmap.height() );
            this->setStyleSheet("QPushButton{border:0px;}");
            this->setIcon(pixmap);
            this->setIconSize(QSize(pixmap.width(),pixmap.height()));
            if(this->max < this->min) //如果小于最小值，重置最大值，并停止定时器
            {
                this->max = 8;
                this->isAnimation = false;
                timer2->stop();
            }
        });

}

void MyCoin::changeFlag(){
    if(this->flag){//如果是正面
        timer1->start(30);
        this->flag = false;
        this->isAnimation = true;
    }else{//如果是反面
        timer2->start(30);
        this->flag = true;
        this->isAnimation = true;
    }
}
void MyCoin::mousePressEvent(QMouseEvent *e){
    if(this->isAnimation||this->isWin==true){
        return;
    }
    else{
        return QPushButton::mousePressEvent(e);
    }
}
